﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VirtualTournaments.Business.Tournaments;

namespace VirtualTournaments.Services.Tournaments
{
    public class Bussiness2ServiceConversor
    {
        
        private static string getTournamentType(Business.Tournaments.Tournament businessObject)
        {
            if (typeof(AllAgainstAllTournament).IsInstanceOfType(businessObject))
                return Tournament.ALLAGAINSTALL_TYPE;
            else
            {
                if (typeof(PlayOffTournament).IsInstanceOfType(businessObject))
                    return Tournament.PLAYOFF_TYPE;

                else throw new ServiceException("Tipo de torneo desconocido");
            }

        }
        public static Tournament ConvertTournament(Business.Tournaments.Tournament businessObject)
        {

            try
            {
                string tournamentType = getTournamentType(businessObject);
                return new Tournament()
                {
                    Name = businessObject.Name,
                    IsActive = businessObject.IsActive,
                    Type = tournamentType

                };
            }

            catch (Exception ex)
            {
                throw new ServiceException("No se puede recuperar el  torneo: " + ex.ToString());
            }

        }
        public static Team ConvertTeam(Business.Tournaments.Team businessObject)
        {

            return new Team(businessObject.Name);

        }
        public static Player ConvertPlayer(Business.Players.Player businessObject)
        {
            String playerName = businessObject.Name.FirstName + " " + businessObject.Name.Surname;
            String teamName = businessObject.Team.Name;
            String role = businessObject.Role.ToString();
            String speed = businessObject.Speed.ToString();
            String precision = businessObject.Precision.ToString();
            String strength = businessObject.Strength.ToString();
            String dribbling = businessObject.Dribbling.ToString();
            return new Player(playerName, teamName, role, speed, precision, strength, dribbling);

        }

        public static TeamStatics ConvertTeamStatic (Business.Tournaments.TeamStatics businessObject)
        {
            return new TeamStatics(businessObject.Team.Name, businessObject.WonMatches, businessObject.DrawMatches, businessObject.LostMatches, businessObject.GoalsFor, businessObject.GoalsAgainst);
        }

        public static Standings ConvertStandings(Business.Tournaments.Standings businessObject)
        {
            List<TeamStatics> teamsStatics = new List<TeamStatics>();
            foreach (Business.Tournaments.TeamStatics teamStatic in businessObject)
                teamsStatics.Add(ConvertTeamStatic(teamStatic));
            return new Standings(teamsStatics);
        }

        public static Match ConvertMatch(Business.Tournaments.Match businessObject)
        {
            if (!businessObject.IsPlayed)
                return new Match(businessObject.Local.Name, businessObject.Visitor.Name);
            else if (businessObject.Result is MatchWithPenaltiesResult)
            {
                MatchWithPenaltiesResult Result = (MatchWithPenaltiesResult)businessObject.Result;
                return new Match(businessObject.Local.Name, businessObject.Visitor.Name, Result.LocalGoals, Result.VisitorGoals, Result.LocalPenaltyGoals, Result.VisitorPenaltyGoals);
            }
            else
                return new Match(businessObject.Local.Name, businessObject.Visitor.Name, businessObject.Result.LocalGoals, businessObject.Result.VisitorGoals);
        }

    }
}
